package com.verlet.springbootdemo.simple.disruptor.netty.server;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.WaitStrategy;
import com.lmax.disruptor.dsl.ProducerType;
import com.verlet.springbootdemo.simple.disruptor.netty.AbstractMessageConsumer;
import com.verlet.springbootdemo.simple.disruptor.netty.MarshallingCodeFactory;
import com.verlet.springbootdemo.simple.disruptor.netty.RingBufferWorkPoolFactory;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author verlet
 * @date 2020/8/17
 */

@Slf4j
public class NettyServer {

    public NettyServer() throws InterruptedException {
        NioEventLoopGroup boosGroup = new NioEventLoopGroup();
        NioEventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(boosGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            socketChannel.pipeline()
                                    .addLast(MarshallingCodeFactory.buildMarshallingDecoder())
                                    .addLast(MarshallingCodeFactory.buildMarshallingEncoder())
                                    .addLast(new ServerHandler());
                        }
                    });

            ChannelFuture channelFuture = serverBootstrap.bind(8765).sync();
            log.info("Server端启动成功。。");
            channelFuture.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            boosGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        AbstractMessageConsumer[] messageConsumers = new AbstractMessageConsumer[8];
        for (int i = 0; i < messageConsumers.length; i++) {
            MessageConsumerImpl messageConsumer = new MessageConsumerImpl("code:serverId:server:" + i);
            messageConsumers[i] = messageConsumer;
        }

        RingBufferWorkPoolFactory.getInstance().initAndStart(ProducerType.MULTI, 1024*1024,new BlockingWaitStrategy(),messageConsumers);

        new NettyServer();
    }

}
